package com.mriassl.stream.receiver;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.mriassl.stream.lib.rabbitmq.RabbitMQConst;
import com.mriassl.stream.service.TransactionService;


@Component
@RabbitListener(queues=RabbitMQConst.QUEUE_FOR_BETTING_REPLY)
public class BettingSuccReplyReceiver {
    private final Logger log = LoggerFactory.getLogger(BettingSuccReplyReceiver.class);
    private Pattern pattern = Pattern.compile("^state:1,busno:(\\w+)$");
    
    @Autowired
    private TransactionService transactionService;
    
    @RabbitHandler
    public void receiveReply(String reply) {
        if (StringUtils.isBlank(reply)) return;
        Matcher matcher = pattern.matcher(reply);
        if (matcher.find()) {
            String busno = matcher.group(1);
            transactionService.finishBettingTransaction(busno);
            log.info("事务回掉成功！！！！！！");
            // 发送投注扣款成功通知
        }
    }

}
